package com.keekus.common.dto.security;

import java.util.Collections;
import java.util.Set;

import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.keekus.common.dto.User;

public class UserDetail extends User implements UserDetails, CredentialsContainer{
	public UserDetail(User p) {
		this.id = p.getId();
		this.account = p.getAccount();
		this.password = p.getPassword();
		this.accountEnabled = p.isAccountEnabled();
		this.accuontExpired = p.isAccuontExpired();
		this.credentialsExpired = p.isCredentialsExpired();
		this.accountLocked = p.isAccountLocked();
		this.authorities = (Set<GrantedAuthority>) p.getAuthorities();
		this.roles = p.getRoles();
	}

	public UserDetail(String account, String password, boolean accountEnabled, boolean accountExpired, boolean credentialsExpired, boolean accountLocked) {
		this.account = account;
		this.password = password;
		this.accountEnabled = accountEnabled;
		this.accuontExpired = accountExpired;
		this.credentialsExpired = credentialsExpired;
		this.accountLocked = accountLocked;
	}

	public UserDetail(String account, String password, boolean accountEnabled, boolean accountExpired, boolean credentialsExpired, boolean accountLocked,
			Set<? extends GrantedAuthority> authorities) {
		this.account = account;
		this.password = password;
		this.accountEnabled = accountEnabled;
		this.accuontExpired = accountExpired;
		this.credentialsExpired = credentialsExpired;
		this.accountLocked = accountLocked;
		this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
	}

	@Override
	public void eraseCredentials() {
		password = null;
	}

	@Override
	public String getUsername() {
		return this.getAccount();
	}

	@Override
	public boolean isAccountNonExpired() {
		return !super.isAccuontExpired();
	}

	@Override
	public boolean isAccountNonLocked() {
		return !super.isAccountLocked();
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return !super.isCredentialsExpired();
	}

	@Override
	public boolean isEnabled() {
		return super.isAccountEnabled();
	}
}
